﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

[Serializable]
public class Weapon
{    
    [Serializable]
    public class RandProperty
    {
        public int isRand = 0;
        public int num = 1;
        public List<PropertyValue<object>> value;
    }
    //武器属性
    public string img = "";
    public string id = "";
    public string suitId = "";//所属套装
    public string name = "";
    private int levelUpLimit = 5;//等级上限
    public List<RandProperty> randProperties = new List<RandProperty>();
    private GMConfig.WeaponType _type = GMConfig.WeaponType.Weapon;
    public GMConfig.WeaponType type {
        set {
            _type = value;
            ResetRandProperties();
        }
        get {
            return _type;
        }
    }
    public List<GMConfig.CardRoleType> roles = new List<GMConfig.CardRoleType>();
    private int _weaponType;
    public int weaponType {
        set {
            _weaponType = value;
            ResetSkill();
        }
        get {
            return _weaponType;
        }
    }
    //每个等级属性不一样用数组存放
    public List<SkillBase> skill = new List<SkillBase>();

    public Weapon()
    {
        weaponType = 0;
        randProperties.Add(new RandProperty());
        randProperties.Add(new RandProperty());
    }
    /// <summary>
    /// 激活随机属性
    /// </summary>
    public void ActiveRandProperty()
    {
        //确定随机属性
        for(int k = 0;k < skill.Count;k++){
            var hasPropertyList = GetHasPropertyList(k);
            for (int j = 0; j < hasPropertyList.Count; j++)
            {
                hasPropertyList[j].val = Utils.GetRandVal(hasPropertyList[j].val.ToString());
                Debug.Log(hasPropertyList[j].GetDiscript() + "   " + hasPropertyList[j].GetVal()[0].ToString());
            }
        }

        randProperties[0].value = new List<PropertyValue<object>>();
        randProperties[1].value = new List<PropertyValue<object>>();
        if(type == GMConfig.WeaponType.Weapon)
        {
            //随机防抗
            if (randProperties[0].isRand == 1) {
                for(int i = 0; i < randProperties[0].num;){
                    if(randProperties[0].value.Count >= EquipmentRand.weaponRandResistance.propertyList.Count) break;
                    var _p = EquipmentRand.weaponRandResistance.GetRandOne();
                    if(!randProperties[0].value.Contains(_p)) {
                        randProperties[0].value.Add(_p);
                        i++;
                    }
                }
            }
            //随机元素
            if (randProperties[1].isRand == 1) {
                for(int i = 0; i < randProperties[1].num;){
                    if(randProperties[1].value.Count >= EquipmentRand.weaponRandElement.propertyList.Count) break;
                    var _p = EquipmentRand.weaponRandElement.GetRandOne();
                    if(!randProperties[1].value.Contains(_p)) {
                        randProperties[1].value.Add(_p);
                        i++;
                    }
                }
            }
        }
        else
        {
            if (randProperties[0].isRand == 1) {
                for(int i = 0; i < randProperties[0].num;){
                    if(randProperties[0].value.Count >= EquipmentRand.weaponRandOther.propertyList.Count) break;
                    var _p = EquipmentRand.weaponRandOther.GetRandOne();
                    if(!randProperties[0].value.Contains(_p)) {
                        randProperties[0].value.Add(_p);
                        i++;
                    }
                }
            }
        }
        //确定随机属性
        for(int k = 0;k < randProperties.Count;k++){
            for(int j = 0;j < randProperties[k].value.Count;j++){
                randProperties[k].value[j].val = Utils.GetRandVal(randProperties[k].value[j].val.ToString());
                Debug.Log(randProperties[k].value[j].GetDiscript() + "   " + randProperties[k].value[j].GetVal()[0].ToString());
            }
        }
    }
    /// <summary>
    /// 重置随机属性
    /// </summary>
    private void ResetRandProperties()
    {
        for(int i = 0;i < randProperties.Count; i++)
        {
            randProperties[i].isRand = 0;
            randProperties[i].value = null;
        }
    }
    /// <summary>
    /// 获得武器名称
    /// </summary>
    /// <returns></returns>
    public string GetWeaponName()
    {
        return LTLocalization.GetText(name);
    }
    /// <summary>
    /// 重置技能
    /// </summary>
    private void ResetSkill()
    {
        // Debug.Log("ResetWeaponSkill");
        skill = new List<SkillBase>();
        for (int i = 0; i < levelUpLimit; i++)
        {
            skill.Add(new WeaponSkill());
        }
    }
    /// <summary>
    /// 使用角色发生改变
    /// </summary>
    /// <param name="roleType"></param>
    public void RolesChange(GMConfig.CardRoleType roleType)
    {
        if (roles.Contains(roleType)) roles.Remove(roleType);
        else roles.Add(roleType);
    }
    /// <summary>
    /// 获得所有属性
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public List<PropertyValue<object>> GetHasPropertyList(int level)
    {
        var hasPropertyList = new List<PropertyValue<object>>();
        if (skill[level] == null) return hasPropertyList;
        var levelSkill = skill[level];
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0") hasPropertyList.Add(pro);
            }
        }
        return hasPropertyList;
    }
    /// <summary>
    /// 获得属性描述
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public string GetPropertyDiscript(int level)
    {
        if (skill[level] == null) return "";
        var levelSkill = skill[level];
        string discript = "";
        bool isAdd = false;
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            isAdd = false;
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0")
                {
                    //大类描述
                    if (!isAdd)
                    {
                        isAdd = true;
                        if (discript != "") discript += ",";
                        discript += levelSkill.propertyList[i].GetDiscript() + ":";
                    }
                    else
                    {
                        if (discript != "") discript += ",";
                    }
                    discript += pro.GetDiscript();
                }
            }
        }
        return discript;
    }
    /// <summary>
    /// 获得所属套装
    /// </summary>
    /// <returns></returns>
    public List<Suit> GetSuits(){
        return SuitMO.GetSuitByWeapon(id);
    }
    /// <summary>
    /// 深拷贝
    /// 修改对象内容不会互相影响
    /// </summary>
    /// <returns></returns>
    public Weapon DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream,this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as Weapon;
        }
    }
}

